import Vue from "vue";
import axios from "axios";
import Router from "@/router/index.js";
Vue.use(Router);
import {
	Notification
} from "element-ui";
import store from "@/store/index";
import QS from 'qs';
let token = localStorage.getItem('token');
/* 环境的切换 */
if (process.env.NODE_ENV == 'development') {
	//开发环境
	axios.defaults.baseURL = '/api';
} else if (process.env.NODE_ENV == 'debug') {
	//调式模式
	axios.defaults.baseURL = '/api';
} else if (process.env.NODE_ENV == 'production') {
	//生产模式
	axios.defaults.baseURL = 'https://api.xingbaihui.com.cn/api/';
}
/* 超时设置 */
axios.defaults.timeout = 10000;
/* 默认请求头 */
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;';
/* token */
if (token != 'null' && token != null) {
	axios.defaults.headers['ApiAuth'] = token;
} else {
	Router.push({
		name: '/'
	})
}
/* 请求拦截 */
axios.interceptors.request.use(
	config => {
		const requestConfig = config;
		if (store.state.token) {
			requestConfig.headers.ApiAuth = store.state.token;
		}
		return requestConfig;
	},
	err => {
		Promise.reject(err);
	}
);
const codeMessage = {
	100: "token错误，请重新登录获取",
	201: "太棒了，保存成功！",
	202: "您的请求已提交，请耐心等待服务器处理！",
	204: "太棒了，删除成功！",
	400: "发出的请求有错误，服务器没有进行新建或修改数据的操作。",
	401: "用户没有权限（令牌、用户名、密码错误）。",
	403: "用户得到授权，但是访问是被禁止的。",
	404: "发出的请求针对的是不存在的记录，服务器没有进行操作。",
	406: "请求的格式不可得。",
	410: "请求的资源被永久删除，且不会再得到的。",
	422: "当创建一个对象时，发生一个验证错误。",
	500: "服务器发生错误，请检查服务器。",
	502: "网关错误。",
	503: "服务不可用，服务器暂时过载或维护。",
	504: "网关超时。"
};


axios.interceptors.response.use(
	response => {
		if (response.status > 200 && response.status < 300) {
			let message = codeMessage[response.status];
			if (response.data && response.data.message) {
				message = response.data.message;
			}
			Notification({
				title: "系统提示",
				type: "success",
				message,
				duration: 2000
			});
		}
		return response;
	},
	error => {
		let message = codeMessage[error.response.status];
		if (error.response.data && error.response.data.message) {
			message = error.response.data.message;
		}
		Notification.error({
			title: "系统提示",
			dangerouslyUseHTMLString: true,
			message,
			duration: 2000
		});
		return Promise.reject(error);
	}
);
export default {
	get: (url, params) => {
		return new Promise((resolve, reject) => {
			axios.get(url, {
				params: params
			}).then(res => {
				resolve(res.data);
			}).catch(err => {
				reject(err.data)
			})
		});
	},
	post: (url, params) => {
		return new Promise((resolve, reject) => {
			axios.post(url, QS.stringify(params))
				.then(res => {
					resolve(res.data);
				})
				.catch(err => {
					reject(err.data)
				})
		});
	}
}
